package com.zx.web.dao.impl;

import com.zx.bean.Order;
import com.zx.utils.DataSourceUtils;
import com.zx.web.dao.OrderDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.dao.impl
 * @Description:
 * @Date 2022/9/15 19:29
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void addOrder(Order order) {
        QueryRunner qr = new QueryRunner();
        String sql = "INSERT INTO `tb_order` (`id`, `uid`, `money`, `status`, `time`, `aid`) VALUES (?, ?, ?, ?, ?, ?);";
        try {
            qr.update(DataSourceUtils.getConnection(),sql,order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<Order> findOrderById(int id) {
        QueryRunner qr = new QueryRunner();
        String sql = "select * from tb_order where uid = ?";
        try {
            return qr.query(DataSourceUtils.getConnection(),sql,new BeanListHandler<>(Order.class),id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
